package com.school.movie.business.web.controller;

import com.school.movie.business.web.model.vo.ForumVo;
import com.school.movie.business.web.model.vo.UserVo;
import com.school.movie.common.utils.BaseConstants;
import com.school.movie.common.bean.PageBean;
import com.school.movie.service.imp.ForumServiceImp;
import com.school.movie.service.imp.SysLogServiceImp;
import com.school.movie.service.imp.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 电影评论控制器
 *
 * @author zhaoren
 */
@Controller
@RequestMapping("/forum")
public class ForumController {

    @Autowired
    ForumServiceImp forumService;
    @Autowired
    SysLogServiceImp logService;
    @Autowired
    UserServiceImpl userService;

    @RequestMapping("/list")
    public String list(int movieId, PageBean pager, HttpSession session, ModelMap map) {

        if (movieId == 0) {
            if (session.getAttribute("movieId") != null) {
                movieId = (Integer) session.getAttribute("movieId");
            }
        } else {
            session.setAttribute("movieId", movieId);
        }
        getParams(movieId, pager, map);
        return "movieForum";
    }

    @PostMapping("/save")
    public String save(HttpServletRequest request, HttpSession session, ForumVo forum, ModelMap map) throws Exception {
        int movieId = (Integer) session.getAttribute("movieId");

        UserVo u = null;
        if (forum.getFromId() != null) {
            u = userService.getById(forum.getFromId());
        }
        if (u == null) {
            map = getParams(movieId, new PageBean(), map);
            map.addAttribute("msg", "该手机号码不是注册用户，评论失败。");
            return "movieForum";
        }
        forum.setMovieId(movieId);
        if (forum.getForumId() > 0) {
            forumService.update(forum);
            logService.add(BaseConstants.LOG_UPDATE, forum.toString());
        } else {
            forumService.add(forum);
            logService.add(BaseConstants.LOG_ADD, forum.toString());
        }
        map = getParams(movieId, new PageBean(), map);
        return "movieForum";
    }

    private ModelMap getParams(int movieId, PageBean pager, ModelMap map) {
        int cnt = forumService.count(movieId);
        pager.setTotalRows(cnt);
        List<ForumVo> movieList = forumService.list(movieId, pager);
        pager.setData(movieList);
        map.addAttribute("pager", pager);
        map.addAttribute("forum", new ForumVo());
        return map;
    }

    //    @RequestMapping("/add")
//    public String add(ModelMap map){
//        map.addAttribute("forum",new ForumVo());
//        return "forum";
//    }

    /**
     * @param forum
     * @param map
     * @return
     */
    @RequestMapping("/update")
    public String update(ForumVo forum, ModelMap map) {
        forum = forumService.getById(forum.getForumId());
        map.addAttribute("forum", forum);
        return "movieForum";
    }

    /**
     * @param ids
     * @param map
     * @param session
     * @return
     */
    @RequestMapping("/del")
    public String del(String ids, ModelMap map, HttpSession session) {
        int movieId = (Integer) session.getAttribute("movieId");
        String[] arr = ids.split(",");
        forumService.del(arr);
        getParams(movieId, new PageBean(), map);
        return "movieForum";
    }
}
